package com.starblink.post.detail.ui

import androidx.fragment.app.Fragment
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.appsflyer.AppsflyerManager
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.ext.commContext
import com.starblink.android.basic.ext.toBean
import com.starblink.android.basic.ext.toJson
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.basic.network.config.PostConfig.postRecommendPage
import com.starblink.android.basic.util.ImageUtils
import com.starblink.basic.route.RoutePage
import com.starblink.rocketreserver.FetchPostsQuery
import com.starblink.rocketreserver.SearchHashtagPostQuery
import com.starblink.rocketreserver.fragment.PostD
import com.starblink.rocketreserver.fragment.PostF
import com.starblink.rocketreserver.type.HashtagPostParam
import com.starblink.rocketreserver.type.PostRecommendQueryParam
import kotlinx.coroutines.launch

class DetailVM : BaseViewModel() {
    var mList: ArrayList<PostF> = arrayListOf()
    var fragmentList: MutableLiveData<ArrayList<Fragment>> = MutableLiveData()

    var originalPostId: String = ""//初始postId
    var sceneId: String = ""//初始postId
    var isLoadingMore: Boolean = false
    var scenePageNo = 1
    var searchType = -1

    /**
     * 加载推荐数据
     */
    fun getRecommend(scene: Int, keyWord: String?) {
        isLoadingMore = true

//        if (scene == RoutePage.Post.SCENE.SEARCH.value) {
//            viewModelScope.launch {
//                val res = SkGqlClient.skQuery(
//                    SearchPostQuery(
//                        content = keyWord ?: "",
//                        contentType = if (searchType == -1) Optional.absent() else searchType.toString()
//                            .optional(),
//                        pageNo = scenePageNo,
//                        size = 30.optional(),
//                    )
//                )
//                val list = res?.data?.searchPost?.map { it.postF } ?: arrayListOf()
//
//            }
//        } else {

        viewModelScope.launch {
            if (scene == RoutePage.Post.SCENE.TOPIC.value) {//获取话题列表
                val response = SkGqlClient.skQuery(
                    SearchHashtagPostQuery(
                        hashtagPostParam = HashtagPostParam(
                            hashtagId = Optional.present(sceneId),
                            postId = Optional.present(""),
                            pageNo = Optional.present(scenePageNo),
                            pageSize = Optional.present(20)
                        )
                    )
                )
                isLoadingMore = false
                if (response?.data != null && response.data!!.searchHashtagPost != null && response.data!!.searchHashtagPost!!.hashtagPostVOF != null) {
                    val hashtagPostVOF = response.data!!.searchHashtagPost!!.hashtagPostVOF
                    var list1 = hashtagPostVOF.posts
                    var list = arrayListOf<Fragment>()
                    list1!!.forEach { e ->
                        if (e != null && e.postF.contentType == "2") {
                            var post = e.postF
                            dealPost(post, list)
                        }
                    }
                    if (list.isNotEmpty()) {
                        scenePageNo++
                        fragmentList.value?.addAll(list)
                        fragmentList.value = fragmentList.value
                    }
                }
            } else {
                val response = SkGqlClient.skQuery<FetchPostsQuery.Data>(
                    FetchPostsQuery(
                        postParams = PostRecommendQueryParam(
                            1,
                            relatedPostId = Optional.Absent,
                            postRecommendPage,
                            12,
                            advertiseId = Optional.present(AppsflyerManager.getADID()),
                            clientTime = Optional.present(System.currentTimeMillis().toString()),
                            isReported = Optional.present(true),
                        ), isPostDetail = false
                    )

                )
                isLoadingMore = false
                if (response?.data != null && response.data!!.fetchRecommendPost != null) {
                    var postList = response.data!!.fetchRecommendPost
                    var list = arrayListOf<Fragment>()
                    postList!!.forEach { e ->
                        var post = e.postF
                        dealPost(post, list)
                    }
                    if (list.isNotEmpty()) {
                        postRecommendPage++
                        fragmentList.value?.addAll(list)
                        fragmentList.value = fragmentList.value
                    }
                }
            }
        }
    }

    fun initData(
        postList: ArrayList<PostF>,
        traceId: String = ""
    ) {
        var list = arrayListOf<Fragment>()
        postList.forEach { post ->
            dealPost(post, list, traceId = traceId)
        }
        if (list.isNotEmpty()) {
            fragmentList.value?.addAll(list)
            fragmentList.value = fragmentList.value
        }
    }

    private fun dealPost(
        post: PostF,
        list: ArrayList<Fragment>,
        traceId: String = ""
    ) {
        if (post.contentType == "1") {//筛选图文类型
            mList.add(post)
            var imgUrl = post.mainPic
            ImageUtils.preloadImage(commContext(), imgUrl)

            var item: PostD = post.toJson().toBean<PostD>()
            list.add(PostDetailFragment.newInstance(item.id, traceId, item))
        }
    }

}